/**
 * 
 */
package com.xbang.attendance.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Row;

import com.xbang.attendance.entity.system.MaintenanceStation;
import com.xbang.attendance.entity.system.Region;
import com.xbang.attendance.entity.system.Warehouse;
import com.xbang.attendance.helper.importexcel.PurchaseImportHelper;
import com.xbang.attendance.service.WarehouseService;
import com.xbang.core.utils.CnToSpellUtils;
import com.xbang.core.utils.ReflectionUtils;

import enumeration.RegionType;

/**
 * @author dev.zhang
 * 
 *         储运商导入
 */
public class WarehouseAnalyzeServiceImpl extends AbstractAnalyzeService {

	private Map<String, String> fieldMapping = new HashMap<String, String>();

	private WarehouseService warehouseService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.xbang.attendance.service.AnalyzeService#analyse(java.io.File)
	 */
	public void analyse(File file) throws Exception {

		POIFSFileSystem fs = null;
		HSSFWorkbook wb = null;

		fs = new POIFSFileSystem(new FileInputStream(file));
		wb = new HSSFWorkbook(fs);

		HSSFSheet sheet = wb.getSheetAt(0);
		setupExcelFieldMappings(sheet);
		List<PurchaseImportHelper> list = new ArrayList<PurchaseImportHelper>();
		for (Row row : sheet) {

			Region region = null;
			MaintenanceStation maintenanceStation = null;
			Map<String, Object> maintenanceStationMap = new HashMap<String, Object>();
			Map<String, Object> regionMap = new HashMap<String, Object>();
			Map<String, Object> warehouseMap = new HashMap<String, Object>();
			Warehouse warehouse = null;
			if (row.getRowNum() == 0) {
				continue;
			}

			if (getCellValue(row.getCell(1)) == null || getCellValue(row.getCell(1)).toString().trim().isEmpty()) {
				continue;
			}

			for (Iterator<Map.Entry<String, Integer>> excelFieldMappingIterator = excelFieldMapping.entrySet().iterator(); excelFieldMappingIterator.hasNext();) {
				Entry<String, Integer> excelFieldMappingEntry = excelFieldMappingIterator.next();
				Object object = getCellValue(row.getCell(excelFieldMappingEntry.getValue()));
				if (object != null && fieldMapping.containsKey(excelFieldMappingEntry.getKey())) {
					String fieldMappingKey = fieldMapping.get(excelFieldMappingEntry.getKey());

					if (fieldMappingKey.split(":").length > 2) {
						String[] fieldMappingArray = fieldMappingKey.split(":");
						if (fieldMappingArray[2].equals("Region")) {
							regionMap.put(fieldMappingArray[0], object);
						} else if (fieldMappingArray[2].equals("MaintenanceStation")) {
							maintenanceStationMap.put(fieldMappingArray[0], object);
						} else {
							warehouseMap.put(fieldMappingArray[0], object);
						}
					}
				}
			}

			
			region = warehouseService.getRegionByName(regionMap.get("name").toString());
			
			if (region == null) {
				region = new Region();
				region.setName(regionMap.get("name").toString());
				region.setNumber(CnToSpellUtils.getFullSpell(regionMap.get("name").toString()));
				region.setRegionType(RegionType.AKZ);
				warehouseService.save(region);
			}
		
			
			warehouse = warehouseService.getWarehouseByName(warehouseMap.get("name").toString());
			if (warehouse == null) {
				warehouse = new Warehouse();
				for (Map.Entry<String, Object> entry : warehouseMap.entrySet()) {
					ReflectionUtils.setFeildByApache(warehouse, entry.getKey(), ReflectionUtils.parseString(entry.getValue()));
				}
			}else{
				for (Map.Entry<String, Object> entry : warehouseMap.entrySet()) {
					ReflectionUtils.setFeildByApache(warehouse, entry.getKey(), ReflectionUtils.parseString(entry.getValue()));
				}
			}
			
			warehouse.setRegion(region);
			if (warehouse.getCode() == null || warehouse.getCode().isEmpty()) {
				warehouse.setCode("WH" + System.currentTimeMillis());
			}
			warehouseService.saveOrUpdate(warehouse);
			
			//没有维修站
			if(!maintenanceStationMap.isEmpty()){
				if(!maintenanceStationMap.containsKey("number") || maintenanceStationMap.get("number") == null)
					continue;
					
				maintenanceStation = warehouseService.getMaintenanceStationByCode(maintenanceStationMap.get("number").toString());
				
				if (maintenanceStation == null) {
					maintenanceStation = new MaintenanceStation();
					for (Map.Entry<String, Object> entry : maintenanceStationMap.entrySet()) {
						ReflectionUtils.setFeildByApache(maintenanceStation, entry.getKey(), ReflectionUtils.parseString(entry.getValue()));
					}
					maintenanceStation.setRegion(region);
					maintenanceStation.setMainWarehouse(warehouse);
					warehouseService.save(maintenanceStation);
				}
			}
			// warehouse
		}
	}

	/**
	 * @return the fieldMapping
	 */
	public Map<String, String> getFieldMapping() {
		return fieldMapping;
	}

	/**
	 * @param fieldMapping
	 *            the fieldMapping to set
	 */
	public void setFieldMapping(Map<String, String> fieldMapping) {
		this.fieldMapping = fieldMapping;
	}

	/**
	 * @param warehouseService
	 *            the warehouseService to set
	 */
	public void setWarehouseService(WarehouseService warehouseService) {
		this.warehouseService = warehouseService;
	}

}
